// seckill.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:[
      {
        name:'法国·AOP  雕花干红葡萄酒750ml',
        url:'http://img0.imgtn.bdimg.com/it/u=3483763287,1877121403&fm=26&gp=0.jpg',
        price:'280',
        discount:'360'
      },
      {
        name:'法国·AOP  雕花干红葡萄酒750ml',
        url:'http://img0.imgtn.bdimg.com/it/u=3483763287,1877121403&fm=26&gp=0.jpg',
        price:'280',
        discount:'360'
      },
      {
        name:'法国·AOP  雕花干红葡萄酒750ml',
        url:'http://img0.imgtn.bdimg.com/it/u=3483763287,1877121403&fm=26&gp=0.jpg',
        price:'280',
        discount:'360'
      },
      {
        name: '法国·AOP  雕花干红葡萄酒750ml',
        url: 'http://img0.imgtn.bdimg.com/it/u=3483763287,1877121403&fm=26&gp=0.jpg',
        price: '280',
        discount: '360'
      }
    ]
  },
  getData(){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
        access_token = getApp().globalData.access_token,
        self = this
    wx.request({
      url: baseUrl + '/Seckill/index',
      method: 'GET',
      data: { access_token},
      success: data => {
        wx.hideLoading()
        if(data.data.code == 200){
          self.setData({
            data: data.data.data,
            timeout: new Array(data.data.data.length)
          })
          self.data.data.forEach( (value,index) => {
            setInterval(function(){
              let str = self.timeout(value.ms_start_time)
              let timeout = self.data.timeout
              timeout[index] = str
              self.setData({timeout})
            },1000)
          })
        }
      }
    })
  },
  timeout(time){
      if(new Date(time).valueOf() < new Date().valueOf()){
        return '秒杀活动已经开始'
      }
      let t = (new Date(time) - new Date()) / 24 / 3600000
      let day = Math.floor(t)
      let hour = Math.floor(t * 24)
      let min = Math.floor((t * 24 - hour) * 60)
      let s = Math.floor(((t * 24 - hour) * 60 - min) * 60)
      let str = day + '天' + hour + '小时' + min + '分' + s + '秒'
      return `距离秒杀活动还有${str}`
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})